package com.ts.system.impl;

import com.ts.comm.SysResult;
import com.ts.constant.OpType;
import com.ts.jdbc.SysDB;
import com.ts.system.dao.UserDao;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 马俊鹏 on 2017/5/17.
 */
@Repository
public class UserDaoImpl implements UserDao{

	@Autowired
	private NamedParameterJdbcTemplate jdbcTemplate;

	/**
	 * 添加用户信息
	 * @param postInfo
	 * @return
	 */
	@Transactional
	public SysResult add(HashMap<String, Object> postInfo) {
		String sql="insert into s_user(fCode,fLoginCode,fName,fDeptCode,fPassword,fIsStop,fDataRange,fOrgCode)";
		sql+=" values(:FCODE,:FLOGINCODE,:FNAME,:FDEPTCODE,:FPASSWORD,:FISSTOP,:FDATARANGE,:FORGCODE)";
		SysResult sysResult=new SysResult();
		try {
			SysDB.update(jdbcTemplate,sql.toString(),postInfo);
		} catch (Exception e) {
			sysResult.onSetErrorMsg(e);
		}
		return  sysResult;
	}

	/**
	 * 更新
	 * @param userInfo
	 * @param fcode
	 * @return
	 */
	@Transactional
	public SysResult update(HashMap<String, Object> userInfo, String fcode) {
		StringBuilder sql = new StringBuilder();
		sql.append("update s_User set fLoginCode = :FLOGINCODE, fDeptCode = :FDEPTCODE, fDataRange = :FDATARANGE, fIsStop = :FISSTOP, fName = :FNAME, fOrgCode = :FORGCODE ");
		sql.append("where fCode = :FCODE");
		SysResult sysResult = new SysResult();
		try {
			SysDB.update(jdbcTemplate,sql.toString(),userInfo);
		} catch (Exception e) {
			sysResult.onSetErrorMsg(e);
		}
		return sysResult;
	}

	/**
	 * 获取单条用户信息
	 * @param fcode
	 * @return
	 */
	public SysResult getUserInfo(String fcode) {
		StringBuilder sql = new StringBuilder();
		sql.append("select u.fcode as fCode, ");
		sql.append("u.fname as fName , ");
		sql.append("u.flogincode as fLoginCode , ");
		sql.append("s.fname as fDeptName, ");
		sql.append("u.fDeptCode as fDeptCode, ");
		sql.append("u.fOrgCode as fOrgCode, ");
		sql.append("sd.fname as fDataRangeName, ");
		sql.append("sd.fcode as fDataRange, ");
		sql.append("decode(u.fisstop,0,'否',1,'是') as fIsStop ");
		sql.append("from s_user u left join s_department s ");
		sql.append("on u.fdeptcode = s.fcode ");
		sql.append("left join s_department sd on sd.fcode = u.fdatarange ");
		sql.append("where u.fcode =:fCode");
		HashMap<String,Object> map = new HashMap<String,Object>();
		SysResult sysResult = new SysResult(jdbcTemplate,sql.toString(),map);
		/*HashMap<String,Object> map = new HashMap<String,Object>();
		try {
			List<Map<String, Object>> rows = SysDB.getRows(jdbcTemplate,sql.toString(),map);
			sysResult.setRows(rows);
		} catch (Exception e) {
			sysResult.onSetErrorMsg(e);
		}*/
		return  sysResult;
	}

	/**
	 * 获取用户列表
	 * @param  fDeptCode 所属机构代码
	 * @return
	 */
	@Transactional
	@RequiresPermissions(value = {"Y04:"+ OpType.View})
	public SysResult query(String fDeptCode,int pageSize,int pageNumber) {
		StringBuilder sql=new StringBuilder();
		sql.append("select ");
		sql.append("u.fcode as FCODE, ");
		sql.append("u.fname as FNAME , ");
		sql.append("u.flogincode as FLOGINCODE , ");
		sql.append("s.fname as FDEPTNAME, ");
		sql.append("u.fDeptCode as FDEPTCODE, ");
		sql.append("u.fOrgCode as FORGCODE, ");
		sql.append("sd.fname as FDATARANGENAME, ");
		sql.append("sd.fcode as FDATARANGE, ");
		sql.append("decode(u.fisstop,0,'否',1,'是') as FISSTOP ");
		sql.append("from s_user u left join s_department s ");
		sql.append("on u.fdeptcode = s.fcode ");
		sql.append("left join s_department sd on sd.fcode = u.fdatarange ");
		sql.append("where u.fdeptcode =:fdeptcode");
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("fdeptcode",fDeptCode);
		SysResult sysResult = new SysResult(jdbcTemplate,sql.toString(),map,pageSize,pageNumber);
		return  sysResult;
	}

	/**
	 * 删除用户
	 * @param fCodeList
	 * @return
	 */
	public SysResult deleteUser(List fCodeList) {
		StringBuilder sql=new StringBuilder();
		sql.append("delete from s_User ");
		sql.append(" where fCode=:fCode");
		SysResult sysResult=new SysResult();
		for(int i = 0 ; i < fCodeList.size(); i ++){
			HashMap<String,Object> map = new HashMap<String,Object>();
			map.put("fCode",fCodeList.get(i));
			try {
				SysDB.update(jdbcTemplate,sql.toString(),map);
			} catch (Exception e) {
				sysResult.onSetErrorMsg(e);
			}
		}
		return  sysResult;
	}

	/**
	 * 通过用户代码查询所在机关的岗位列表
	 * @param fCode
	 * @return
	 */
	public SysResult getDeptPost(String fCode) {
		StringBuilder sql = new StringBuilder();
		sql.append("select rownum as frownum, p.fcode,p.fname,decode(p.fisstop,'0','否','1','是') as fisstop ");
		sql.append("from s_post p ");
		sql.append(" where p.forgcode = ");
		sql.append("(select u.forgcode from s_user u where u.fcode = :fCode)");
		SysResult sysResult = new SysResult();
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("fCode",fCode);
		try {
			List<Map<String, Object>> rows = SysDB.getRows(jdbcTemplate, sql.toString(),map);
			sysResult.setRows(rows);
		} catch (Exception e) {
			sysResult.onSetErrorMsg(e);
		}
		return  sysResult;
	}

	/**
	 * 查询用户岗位列表
	 * @param fCode
	 * @return
	 */
	public SysResult getUserPostList(String fCode) {
		StringBuilder sql = new StringBuilder();
		sql.append("select t.fpostcode from S_USERPOST t where t.fusercode = :fCode");
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("fCode",fCode);
		SysResult sysResult = new SysResult();
		try {
			List<Map<String, Object>> rows = SysDB.getRows(jdbcTemplate, sql.toString(),map);
			sysResult.setRows(rows);
		} catch (Exception e) {
			sysResult.onSetErrorMsg(e);
		}
		return  sysResult;
	}

	/**
	 * 设置用户的岗位
	 * @param postCodeList
	 * @param userCode
	 * @return
	 */
	@Transactional
	public SysResult setUserPost(List<String> postCodeList, String userCode) {
		SysResult sysResult = new SysResult();
		//先删除
		StringBuilder deleteSql = new StringBuilder();
		deleteSql.append("delete from s_userpost s where s.fusercode = :userCode ");
		HashMap<String,Object> deleteMap = new HashMap<String,Object>();
		deleteMap.put("userCode",userCode);
		try {
			SysDB.update(jdbcTemplate,deleteSql.toString(),deleteMap);
		} catch (Exception e) {
			sysResult.onSetErrorMsg(e);
		}


		//添加
		StringBuilder addSql = new StringBuilder();
		addSql.append("insert into s_userpost ( fusercode,fpostcode )");
		addSql.append("values(:userCode,:postCode)");
		for(String postCode:postCodeList){
			HashMap<String,Object> addMap = new HashMap<String,Object>();
			addMap.put("userCode",userCode);
			addMap.put("postCode",postCode);
			try {
				SysDB.update(jdbcTemplate,addSql.toString(),addMap);
			} catch (Exception e) {
				sysResult.onSetErrorMsg(e);
			}
		}
		return sysResult;
	}
}
